/**
 * 自定义查询参数
 */
export interface CustomQueryParams {
    conditions?: CustomQueryCondition[]
    [prop: string]: any
}

/**
 * 查询条件
 */
export interface CustomQueryCondition {
    property: string
    mode: CustomQueryMode
    values: any[]
}

/**
 * 查询匹配条件
 */
export type CustomQueryMode = "EQ" | "GT" | "EGT" | "LT" | "ELT" | "BETWEEN" | "LIKE" | "PRE_LIKE" | "IN"

/**
 * 自定义查询配置
 */
export interface CustomQueryProps {
    // 属性名
    property: string
    // 字段名
    column?: string
    // 查询匹配条件
    customQueryMode?: CustomQueryMode
}

/**
 * 组装自定义查询条件
 */
export function buildQuery(props: CustomQueryProps[], values: Recordable): CustomQueryParams {
    const propMap: Recordable<CustomQueryProps> = {}
    props.forEach(p => (propMap[p.property] = p))
    const params: CustomQueryParams = {}
    const conditions: CustomQueryCondition[] = []
    Object.keys(values).forEach(k => {
        const prop = propMap[k]
        if (prop && prop.customQueryMode) {
            const property = prop.column ?? prop.property
            conditions.push({ property, mode: prop.customQueryMode, values: values[k] })
        } else {
            params[k] = values[k]
        }
    })
    params.conditions = conditions
    return params
}
